local skel = fk.CreateSkill {
  name = "lb__huaiwu",
  tags = { Skill.Compulsory, },
}

Fk:loadTranslationTable {
  ["lb__huaiwu"] = "怀无",
  [":lb__huaiwu"] = "<b>锁定技</b>，你视为装备着你首次使用的武器牌。",

  ["@lb__huaiwu"] = "怀无",

  ["$lb__huaiwu1"] = "流淌吧，过往的刀光。",
  ["$lb__huaiwu2"] = "我拔刀的理由，从前、如今、往后，只有一个。",
}

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill("lb__huaiwu") then return false end
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if (move.to == player and move.toArea == Card.PlayerEquip and player:getMark("lb__huaiwu") == 0)
            or (move.from == player and info.fromArea == Card.PlayerEquip) then
          return Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        local card = Fk:getCardById(info.cardId) ---@cast card EquipCard
        if move.to == player and move.toArea == Card.PlayerEquip and
            card.sub_type == Card.SubtypeWeapon and player:getMark("lb__huaiwu") == 0 then
          skel:onAcquire(player, false)
        elseif move.from == player and info.fromArea == Card.PlayerEquip and
            card.sub_type == Card.SubtypeWeapon and card.name == player:getMark("lb__huaiwu") then
          local skills = card:getEquipSkills(player)
          if #skills > 0 then
            room:handleAddLoseSkills(player, table.concat(table.map(skills, Util.NameMapper), "|"), skel.name, false)
          end
        end
      end
    end
  end,
})

skel:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:getMark("lb__huaiwu") ~= 0 and from:hasSkill("lb__huaiwu") then
      local card = Fk:cloneCard(from:getMark("lb__huaiwu"))
      if card.sub_type == Card.SubtypeWeapon then
        local n = card.attack_range
        local nums = {}
        if #from:getEquipments(Card.SubtypeWeapon) > 0 then
          for _, id in ipairs(from:getEquipments(Card.SubtypeWeapon)) do
            table.insert(nums, Fk:getCardById(id).attack_range)
          end
        end
        if #nums > 0 then
          local orig = math.max(table.unpack(nums))
          if n > orig then
            return n - orig
          else
            return 0
          end
        else
          return n
        end
      end
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local name = ""
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for i = 1, #e.data, 1 do
      local move = e.data[i]
      if move.toArea == Card.PlayerEquip and move.to == player then
        for j = 1, #move.moveInfo, 1 do
          local info = move.moveInfo[j]
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeEquip and card.sub_type == Card.SubtypeWeapon then
            name = card.name
            return true
          end
        end
      end
    end
  end, Player.HistoryGame)
  if name ~= "" then
    local card = Fk:cloneCard(name) ---@cast card EquipCard
    local skills = card:getEquipSkills(player)
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(table.map(skills, Util.NameMapper), "|"), skel.name, false)
    end
    --room:handleAddLoseSkills(player, card.equip_skill.name, nil, false, false)
    room:setPlayerMark(player, "lb__huaiwu", name)
    room:setPlayerMark(player, "@lb__huaiwu", Fk:translate(name))
    player:broadcastSkillInvoke(skel.name)
    player.room:notifySkillInvoked(player, skel.name)
  end
end)

skel:addLoseEffect(function(self, player, is_death)
  local room = player.room
  if player:getMark("lb__huaiwu") ~= 0 then
    room:setPlayerMark(player, "lb__huaiwu", 0)
    room:setPlayerMark(player, "@lb__huaiwu", 0)
  end
end)

return skel
